/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BLL;

import DAL.ConnectData;
import DTO.User;
import java.sql.ResultSet;
import java.util.ArrayList;


/**
 *
 * @author Vien-IT
 */
public class BUser {
    ConnectData condata ;

    public BUser() {
        condata = new ConnectData();
    }
    
    public ArrayList<User> getAll(String query)
    {
        ArrayList<User> users  = new ArrayList<User>();
        User user = null;
        try {
            
            ResultSet rs = condata.executeQuery(query);
            while(rs.next())
            {
                user = new User();
                user.setUserId(rs.getInt("userId"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setEmail(rs.getString("email"));
                user.setAddress(rs.getString("address"));
                user.setNumberPhone(rs.getString("numberPhone"));
                users.add(user);
               
            }
            
        } catch (Exception ex) {
            System.out.println (ex.toString());
        }
        finally
        {
        	 this.condata.close();
        }
        
        
        return users.isEmpty() ? null : users;
        
    }
    
    
    public ArrayList<User> getAll()
    {
        String sql = "select * from tblUser";
        return this.getAll(sql);
    }
    
    public User getById(int userId)
    {
        String sql = "select * from tblUser where userId = "+userId+"";
        ArrayList<User> users = this.getAll(sql);
        return users == null ? null : users.get(0);
    }
    
    public User getByUsername(String username)
    {
        String sql = "select * from tblUser where username = '"+username+"'";
        ArrayList<User> users = this.getAll(sql);
        return users == null ? null : users.get(0);
    }
    
    public boolean isUser(String username, String password)
    {
        boolean check = false;
        ArrayList<User> users = getAll();
        for(User user: users)
        {
            if(username.equals(user.getUsername())&&password.equals(user.getPassword()))
                
            {
                check = true;
                break;
            }
        }
        
        return check; 
    }
    
    
    public boolean insert(User user)
    {
        boolean check = false;
        int rs = -1;
        String sql = "insert into tblUser (username, password, email, address, numberPhone)"
                + "values ('"+user.getUsername()+"','"+user.getPassword()+"', '"+user.getEmail()+"', '"+user.getAddress()+"', '"+user.getNumberPhone()+"')";
        try {
         rs = condata.executeUpdate(sql);
            
            if(rs > 0)
                check = true;
        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
         
        return check ;
    }
    
    
    
    
}
